//获取轮播图
async function getBanner() {
  //请求
  return await new Promise((resolve, reject) => {
    wx.request({
      url: 'https://autumnfish.cn/banner',
      success(res) {
        const urls = res.data.banners.map(item => item.imageUrl)
        resolve(urls)
      }
    })
  })

}
//获取新歌列表
async function getNewMusicList(type) {
  return await new Promise((resolve, reject) => {
    wx.request({
      url: 'https://autumnfish.cn/top/song?type=' + type,
      success(res) {
        console.log("success")
        let list = res.data.data.map(item => {
          let artistsName = item.artists.map(artist => artist.name).join('/')
          let info = {
            albumPic: item.album.blurPicUrl + "?param=200y200",
            albumName: item.album.name,
            name: item.name,
            alias: item.alias.length > 0 ? item.alias[0] : '',
            flag: (item.privilege.flag & 64) == 0,
            artistsName: artistsName,
            id: item.id
          }
          return info
        })
        resolve(list)
      }
    })
  })
}
//获取歌曲播放地址
async function getMusicPlayUrl(id) {
  return checkMusicUrl(id).then(res=>getMusicPlayUrlOrNull(id))
}
//获取歌曲url 可能获取不到 url
async function getMusicPlayUrlOrNull(id) {
  return await new Promise((resolve, reject)=>{
    wx.request({
      url: 'https://autumnfish.cn/song/url?id=' + id,
      success(res) {
        console.log("xxxxx",res)
        if (res.data.data.length > 0) {
          const mp3Url = res.data.data[0].url
          //存储信息
          resolve(mp3Url)
        }
      }
    })
  })
}

async function checkMusicUrl(id){
  return await new Promise((resolve, reject)=>{
    wx.request({
      url: 'https://autumnfish.cn/check/music?id=' + id,
      success(res) {
        if(res.data.success){
          resolve()
        }else{
          reject(res.data.message)
        }
      }
    })
  })
}

export {
  getBanner,
  getNewMusicList,
  getMusicPlayUrl
}